# shellcheck shell=sh disable=SC3043

___x_cmd_hub_account_login(){
    param:void
    local op="$1"
    local _SELECT
    case "$op" in
        *@*)                    ___x_cmd_hub_account_login_email_witharg "$@" ;;
        wx|weixin)              ___x_cmd_hub_account_login_weixin_qr ;;
        tg|telegram)            ___x_cmd_hub_account_login_telegram_qr ;;
        "")
                ___x_cmd_ui_select "_SELECT" \
                    "Login Methods:" \
                        "Open Browser to x-cmd.com, you can login via Github/Gitee/Weixin" \
                        "Login With Weixin QR" \
                        "Login With Telegram" \
                        "Login With Email Verification"
                        # "Login With OneTime Password"    # SMS, Telegram, Email, Weixin

                case "$_SELECT" in
                    1)          ___x_cmd_hub_account_login_official_site "$@" ;;
                    2)          ___x_cmd_hub_account_login_weixin_qr     "$@" ;;
                    3)          ___x_cmd_hub_account_login_telegram   "$@" ;;
                    4)          ___x_cmd_hub_account_login_email         "$@" ;;
                    *)          ;;
                esac
                ;;
        *)
                ___x_cmd_hub_account_login_official_site "$@" ;;
    esac
}

# TODO: move to website login
___x_cmd_hub_account_login_telegram(){
    local token_to_activate
    token_to_activate="$(___x_cmd_hub___util_token_generate)"

    printf "Open the url in browser to login: https://hub.x-cmd.com/s/tl/%s\nthen press [Enter] to continue: " "${token_to_activate}"
    read -r _

    ___x_cmd_hub___util_save_token_with_ui "$token_to_activate"
}

___x_cmd_hub_account_login_weixin_qr(){
    local token_to_activate
    token_to_activate="$(___x_cmd_hub___util_token_generate)"

    ___x_cmd_hub_qrcode "$(___x_cmd_hub_weixin_login_url "${token_to_activate}")"
    printf "Please scan the QR code above, then press [Enter] to continue: "
    read -r _

    ___x_cmd_hub___util_save_token_with_ui "$token_to_activate"
}

___x_cmd_hub_account_login_official_site(){

    local token_to_activate
    token_to_activate="$(___x_cmd_hub___util_token_generate)"

    # TODO: to change xlogin to login
    ___x_cmd_http_browse "$___X_CMD_HUB_SERVICE_URL/xlogin?token_to_activate=${token_to_activate}"
    printf "Please login to hub.x-cmd.com, then press [Enter] to continue: " >&2
    read -r _

    ___x_cmd_hub___util_save_token_with_ui "$token_to_activate"
}

# Section: email(frozen)
___x_cmd_hub_account_login_email_witharg(){
    local email="${1:?Provide email}"
    local token_to_activate
    token_to_activate="$(___x_cmd_hub___util_token_generate)"

    local res
    if ! res="$(NO_AUTH=1 ___x_cmd_hub___util_curl post /api/v0/account/email/login -- "tokenToActivate=$token_to_activate" "email=$email")" ; then
        ___x_cmd_hub___util_handle_resp false "$res" "Failed to login with email $email"
        return 1
    fi
    hub:debug "Login with email: $email, res: $res"
    printf "%s" "Please check your email $email to approve, then press [Enter] to continue: " >&2
    read -r _

    ___x_cmd_hub___util_save_token_with_ui "$token_to_activate"
}

___x_cmd_hub_account_login_email(){
    local email="${1}"
    if [ -z "$email" ]; then
        printf "%s" "Email: "
        read -r email
    fi
    ___x_cmd_hub_account_login_email_witharg "$email"
}

# EndSection
